var n = 0, flag = true, JSONData = "";
var appdata = "", ordertype = "", orderstu = "", account = "", num = 0, summoney = 0.0, total = 0, pagelen = 0;
var newsbox = $(".datalist");
/*初始化*/
var counter = 1;
/*计数器*/
var pageStart = 0;
/*offset*/
var pageSize = 1;
/*size*/

/*首次加载*/
getData(pageStart, pageSize);

/*监听加载更多*/
$(document).on('click', '.js-load-more', function () {
    counter++;
    pageStart = counter * pageSize;

    getData(pageStart, pageSize);
});

function getData(offset, size) {
    $.ajax({
        url: "orderlist",
        type: "POST",
        dataType: "JSON",
        data: {"form": counter, "AlltoNow": $(".AlltoNow").val(), "cj": $(".cj").val()},
        success: function (data) {

            /*使用for循环模拟SQL里的limit(offset,size)*/
            console.log(data)
            JSONData = JSON.parse(data);
            /* $(".datalist").html('') */
            var avtiv = JSONData.MerAcv;
            var Ordertype = JSONData.OrderType;
            var Order = JSONData.Page.pageList;
            var Orderstuas = JSONData.OrderStatus;
            var Merser = JSONData.MerSer;
            var Vouchers = JSONData.Vouchers;
            total = JSONData.Page.pageSize
            for (var o = 0; o < Order.length; o++) {
                for (var i = 0; i < Ordertype.length; i++) {
                    if (Order[o].orderType == Ordertype[i].typeTypeid) {
                        ordertype = Ordertype[i].typeTypevalue
                    }
                }

                for (var j = 0; j < Orderstuas.length; j++) {
                    if (Order[o].orderState == Orderstuas[j].typeTypeid) {
                        orderstu = Orderstuas[j].typeTypevalue
                    }
                }

                for (var k = 0; k < Merser.length; k++) {
                    if (Order[o].merserId == Merser[k].id && Order[o].merchantsId == Merser[k].merchId) {
                        account = Merser[k].merchSerAccount
                    }
                }
                num = Order[o].orderNum

                for (var p = 0; p < Vouchers.length; p++) {
                    if (Order[o].orderVouchers == Vouchers[p].vouchersId) {
                        if (Order[o].orderVouchers == 0) {
                            summoney = Order[o].orderPrice
                        } else {
                            if (Order[o].orderPrice >= Vouchers[p].vouchersFull) {
                                summoney = Order[o].orderPrice - Vouchers[p].vouchersPrice
                            } else {
                                summoney = Order[o].orderPrice
                            }
                        }

                    }
                }
                appdata += "  <div class='card'><div class='border-bot cardcell'><div class='flex'><h2 class='flexitem ellipsis'>" + ordertype + "</h2><div class='clearfix fr'><spa class='fl'></span><span class='fl' style='padding-left: 8px;font-size:18px; color:#039be5;'>" + orderstu + "</span><a href='javascript:;' class='fl delete'></a></div></div><div class='goodsinfo'><a href='#' class='flex'><img src='/" + avtiv + "'  class='goodspic'><p style='font-size:16px' class='flexitem fontsmall'>" + account + "</p></a></div><p  style='font-size:18px' class='text-right fontsmall'>共<span style='color:red'>" + num + "</span>件商品</p><p style='font-size:18px'  class='text-right fontsmall'>实付款：<b style='color:red;'>￥" + summoney + "</b></p></div><div class='cardcell text-right'><a href='orderinfo?orderid=" + Order[o].orderId + "' class='btn-outline'>查看详情</a></div></div><div class='blockH'></div>"
                newsbox.html("")
                newsbox.append(appdata);
            }
            if (Order.length == 0) {
                appdata = "<img  style='position: relative;top: 100px;left: 15px;opacity:0.5;' src='/images/kong.png'>"
                newsbox.html("")
                newsbox.append(appdata);
            }

            /*隐藏more按钮*/
            if (JSONData.Page.hasNextPage == false) {
                $(".js-load-more").hide();
            } else {
                $(".js-load-more").show();
            }
        },
        error: function (xhr, type) {

        }
    });
}	
